//
//  SetQA.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/2/20.
//

import UIKit
/**
 swift用Set声明集合
 */
class SetQA: NSObject {
    static func QA() {
        let item = QA349()
        let array = [4,9,5]
        let array1 = [9,4,9,8,4];
        let result = item.intersection(array, array1)
        print(result)
    }
    
    
    static func setUser() {
        //通过初始化器语法来创建一个确定类型的空合集
        var letters = Set<Int>()
        letters.insert(12)
        print("letters is of type \(type(of: letters)) with \(letters.count) items")
        //使用数组字面量来初始化一个合集，算是一种写一个或者多个合集值的快捷方式
        var fruits : Set<String> = ["apple", "banana", "Grape", "Pear"]
        //也可以写的简短些，以为有类型推断
        var fruits1 : Set = ["apple", "banana", "Grape", "Pear", "plum", "Chestnut"]
        
        //判断是否为空
        print("I have \(fruits.count) fruits in my home.")
        //I have 4 fruits in my home.
        
        //检查集合是否为空
        let isEmpty = fruits.isEmpty
        print("isEmpty = \(isEmpty)")
        //isEmpty = false
        
        //添加新的元素
        fruits.insert("Peach")
        
        //是否包含某个元素
        print("Fruits is \(fruits.contains("apple")) contains apple.")
        //Fruits is true contains apple.
        
        //移除某个元素
        fruits.remove("apple")
    }
    
    //执行合集操作
    static func executiveEnsembel() {
        
        let fruits1 : Set = ["apple", "banana", "Grape", "Pear", "Tomatoes"]
        let fruits2 : Set = ["apple", "banana", "Grape", "Pear", "plum", "Chestnut"]
        
        //交集
        let intersects = fruits1.intersection(fruits2)
        print("intersects = \(intersects)")
        // intersects = ["Pear", "Grape", "banana", "apple"]
        
        //两个集合的补集
//        let exclusiverOr = fruits1.exclusiveOr(fruits2)
//        print("exclusiverOr = \(exclusiverOr)")

        //并集
        let union = fruits1.union(fruits2)
        print("union = \(union)")
        //union = ["Tomatoes", "Pear", "banana", "Grape", "Chestnut", "plum", "apple"]
        
        //a集合中出去不包含b集合的部分
        let subtract = fruits1.subtracting(fruits2)
        print("subtract = \(subtract)")
        //subtract = ["Tomatoes"]
        
        let fruits3 : Set = ["apple", "banana", "Grape", "Pear"]
        let fruits4 : Set = ["apple", "banana", "Grape", "Pear", "plum", "Chestnut"]
        
        
        //是否是子集
        let isSubSet = fruits3.isSubset(of: fruits4)
        print("Fruits3 is subset fruits4? Answer is \(isSubSet)")
        //Fruits3 is subset fruits4? Answer is true
        
        //是否是父集
        let isSuperSet = fruits3.isSuperset(of: fruits4)
        print("Fruits3 is supset fruits4? Answer is \(isSuperSet)")
        //Fruits3 is supset fruits4? Answer is false
        
        //a合集是否为b合集的子集或者超集，但并不相等
        let fruits5 = fruits3.union(fruits4)
        let isStrictSubset = fruits3.isStrictSubset(of: fruits5)
        print("Fruits3 is fruits5 strictSubset? Answer is \(isStrictSubset)")
        //Fruits3 is fruits5 strictSubset? Answer is true
        
        //判断两个集合是否有完全不同的值
        let isDisjoint = fruits3.isDisjoint(with: fruits4)
        print("Fruits3 is different fruits4? Answer is \(isDisjoint)")
        // Fruits3 is different fruits4? Answer is false
    }
}
